<%-- 
    Document   : MessageThread
    Created on : Dec 4, 2011, 4:57:53 PM
    Author     : hendrikde
--%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Message</title>
</head>

<body><table width="800" border="0">
  <tr>
    <th scope="col"><label name="Firstname" value="" />
    <label name="Lastname" value="" /></th>
  </tr>
  <%
        String message_id = request.getParameter("MESS");
        String other_person = request.getParameter("PERS");
        int is_sender = Integer.parseInt(request.getParameter("SEND"));
 
        String subject = new String();
    
       DBClasses.GetConnection gc = new DBClasses.GetConnection();
       java.sql.Connection conn = gc.getConnection();

       try {
           java.sql.Statement stmt = conn.createStatement();
           String query = "SELECT C.Firstname, M.Subject, M.Content, M.MessageID "
                   + "FROM Customer C, Messages M "
                   + "WHERE C.CustomerID='" + other_person + "' AND M.MessageID ='" + message_id + "'";
           
           java.sql.ResultSet rs = stmt.executeQuery(query);
           
           while (rs.next()) {
                subject = rs.getString(2);
           %>
  <tr>
    <td><table width="800" border="0">
      <tr>
        <th scope="col">
         <% if (is_sender==0) {%>
        <label name="Firstname" value="From <%rs.getString(1);%>" />From <%=rs.getString(1)%>
        <%;}
        else {%>
           <label name="Firstname" value="To <%rs.getString(1);%>" />To <%=rs.getString(1)%>
        <%;}%>
        </th>
                                   }
        <th scope="col"><table width="800" border="0">
          <tr>
            <th scope="col"><label name="Subject" value="<%rs.getString(2);%>" /><%=rs.getString(2)%></th>
          </tr>
          <tr>
            <td><label name="Content" value="<%rs.getString(3);%>"/><%=rs.getString(3)%></td>
          </tr>
        </table></th>
      </tr>
    </table></td>
  </tr>
            <%
                       }
           if (is_sender == 0)
            conn.createStatement().execute("UPDATE Messages M SET M.MessageRead=1 WHERE M.MessageID='" + message_id +"'");
                     }
       catch (Exception e) {
           e.printStackTrace();
           response.sendRedirect("MessageIndex.jsp");
       }
       finally
    {
        try{
            conn.commit();
            conn.close();
        }
        catch(Exception ee)
                               {
            ee.printStackTrace();
        }
    }
            %>
</table>
<form name="Reply" action="NewMessage.jsp">
    <input type="hidden" name="PERS" value="<%=other_person%>"/>
    <input type="hidden" name="SUBJ" value="<%="RE: " + subject%>"/>
    <input type="submit" value="Reply"/>
</form>
    <input type="button" name="return" value="Return" onclick="window.location='MessageIndex.jsp'"/>
</body>
</html>
